package com.cnswhy.cloud.common.rabbitmq.service;

import com.alibaba.fastjson.JSON;
import com.cnswhy.cloud.common.rabbitmq.constant.DLXMQConstant;
import com.cnswhy.cloud.common.rabbitmq.constant.MQConstant;
import com.cnswhy.cloud.common.rabbitmq.model.DLXMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
public class MessageService {
    @Autowired
    RabbitTemplate rabbitTemplate;

    /*public void send(String queueName, String message) {
        try {
            rabbitTemplate.convertAndSend(queueName, message);
        } catch (AmqpException e) {
            e.printStackTrace();
        }

    }*/

    public void sendByObject(String queueName, Object object) {
        try {
            rabbitTemplate.convertAndSend(queueName, JSON.toJSONString(object));
        } catch (AmqpException e) {
            e.printStackTrace();
        }


    }
    public void sendQueueByObject(String queueName, Object object) {
        try {
            rabbitTemplate.convertAndSend(queueName,queueName + MQConstant.QUEUE, object);
        } catch (AmqpException e) {
            e.printStackTrace();
        }


    }

    /*public void send(String queueName, String message, long times) {
        try {
            DLXMessage dlxMessage = new DLXMessage(queueName, message, times);
            MessagePostProcessor processor = new MessagePostProcessor() {
                @Override
                public Message postProcessMessage(Message message) throws AmqpException {
                    message.getMessageProperties().setExpiration(times + "");
                    return message;
                }
            };
            dlxMessage.setExchange(MQConstant.DEFAULT_EXCHANGE);
            rabbitTemplate.convertAndSend(MQConstant.DEFAULT_EXCHANGE, MQConstant.DEFAULT_DEAD_LETTER_QUEUE_NAME, JSON.toJSONString(dlxMessage), processor);
        } catch (AmqpException e) {
            e.printStackTrace();
        }
    }*/
    public void sendAndQueryTimes(String exchange, Object content, long queryTimes) {
        try {
            long unitSeconds = 10 * 1000; // 单元： 10s
            //queryTimes 从 0 开始
            long queryTimesExtra = queryTimes + 1;

            long totalSeconds = unitSeconds ;
            if(queryTimesExtra == 1){
                totalSeconds *= 1;
            }else if(queryTimes == 2){
                totalSeconds *= 2;
            }else if(queryTimes == 3){
                totalSeconds *= 4;
            }else if(queryTimes == 4){
                totalSeconds *= 8;
            }else if(queryTimes == 5){
                totalSeconds *= 16;
            }else if(queryTimes == 6){
                totalSeconds *= 32;
            }else if(queryTimes == 7){
                totalSeconds *= 64;
            }else if(queryTimes == 8){
                totalSeconds *= 128;
            }else if(queryTimes == 9){
                totalSeconds *= 256;
            }else if(queryTimes == 10){
                totalSeconds *= 512;
            }else if(queryTimes == 11){
                totalSeconds *= 1024;
            }else if(queryTimes > 11){
                totalSeconds *= 1024;
            }else{
            }
            DLXMessage dlxMessage = new DLXMessage(exchange,exchange + MQConstant.QUEUE, JSON.toJSONString(content),content.getClass(), totalSeconds);
            String totalSecondsString = totalSeconds + "";
            MessagePostProcessor processor = new MessagePostProcessor() {
                @Override
                public Message postProcessMessage(Message message) throws AmqpException {
                    message.getMessageProperties().setExpiration(totalSecondsString);
                    return message;
                }
            };

            if(queryTimes == 1){
                rabbitTemplate.convertAndSend(MQConstant.DEFAULT_EXCHANGE, DLXMQConstant.DEAD_LETTER_QUEUE_10, JSON.toJSONString(dlxMessage), processor);
            }else if(queryTimes == 2){
                rabbitTemplate.convertAndSend(MQConstant.DEFAULT_EXCHANGE, DLXMQConstant.DEAD_LETTER_QUEUE_20, JSON.toJSONString(dlxMessage), processor);
            }else if(queryTimes == 3){
                rabbitTemplate.convertAndSend(MQConstant.DEFAULT_EXCHANGE, DLXMQConstant.DEAD_LETTER_QUEUE_40, JSON.toJSONString(dlxMessage), processor);
            }else if(queryTimes == 4){
                rabbitTemplate.convertAndSend(MQConstant.DEFAULT_EXCHANGE, DLXMQConstant.DEAD_LETTER_QUEUE_80, JSON.toJSONString(dlxMessage), processor);
            }else if(queryTimes == 5){
                rabbitTemplate.convertAndSend(MQConstant.DEFAULT_EXCHANGE, DLXMQConstant.DEAD_LETTER_QUEUE_160, JSON.toJSONString(dlxMessage), processor);
            }else if(queryTimes == 6){
                rabbitTemplate.convertAndSend(MQConstant.DEFAULT_EXCHANGE, DLXMQConstant.DEAD_LETTER_QUEUE_320, JSON.toJSONString(dlxMessage), processor);
            }else if(queryTimes == 7){
                rabbitTemplate.convertAndSend(MQConstant.DEFAULT_EXCHANGE, DLXMQConstant.DEAD_LETTER_QUEUE_640, JSON.toJSONString(dlxMessage), processor);
            }else if(queryTimes == 8){
                rabbitTemplate.convertAndSend(MQConstant.DEFAULT_EXCHANGE, DLXMQConstant.DEAD_LETTER_QUEUE_1280, JSON.toJSONString(dlxMessage), processor);
            }else if(queryTimes == 9){
                rabbitTemplate.convertAndSend(MQConstant.DEFAULT_EXCHANGE, DLXMQConstant.DEAD_LETTER_QUEUE_2560, JSON.toJSONString(dlxMessage), processor);
            }else if(queryTimes == 10){
                rabbitTemplate.convertAndSend(MQConstant.DEFAULT_EXCHANGE, DLXMQConstant.DEAD_LETTER_QUEUE_5120, JSON.toJSONString(dlxMessage), processor);
            }else if(queryTimes == 11){
                rabbitTemplate.convertAndSend(MQConstant.DEFAULT_EXCHANGE, DLXMQConstant.DEAD_LETTER_QUEUE_10240, JSON.toJSONString(dlxMessage), processor);
            }else if(queryTimes > 11){
                rabbitTemplate.convertAndSend(MQConstant.DEFAULT_EXCHANGE, DLXMQConstant.DEAD_LETTER_QUEUE_5120, JSON.toJSONString(dlxMessage), processor);
            }else{
                rabbitTemplate.convertAndSend(MQConstant.DEFAULT_EXCHANGE, MQConstant.DEFAULT_DEAD_LETTER_QUEUE_NAME, JSON.toJSONString(dlxMessage), processor);
            }
        } catch (AmqpException e) {
            e.printStackTrace();
        }

    }
}
